package com.epam.facebook_client;

import java.util.Map;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Toast;

import com.epam.facebook_client.services.ServiceCallbackListener;
import com.epam.facebook_client.services.ServiceHelper;
import common.utils.StateNetworkUtil;

public abstract class BaseServiceActivity extends FragmentActivity implements ServiceCallbackListener {

	private ServiceHelper serviceHelper;
	private int requestId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		serviceHelper = ServiceHelper.get(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		serviceHelper.addListener(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		serviceHelper.removeListener(this);
	}

	protected ServiceHelper getServiceHelper() {
		return serviceHelper;
	}

	protected void startServiceHelper(Map<String, String> parameter, View view, String action) {
		if (StateNetworkUtil.isOnline(this)) {
			if (getServiceHelper().isPending(getRequestId())) {
				return;
			}
			if (view != null)
				view.setVisibility(View.VISIBLE);
			setRequestId(getServiceHelper().startService(action, parameter));
		} else {
			if (view != null)
				view.setVisibility(View.GONE);
			Toast.makeText(this, "No connection to Internet", Toast.LENGTH_LONG).show();
		}
	}

	protected int getRequestId() {
		return requestId;
	}

	private void setRequestId(int requestId) {
		this.requestId = requestId;
	}
}
